home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Documentation / develop / develop Issue 7 / develop 7 code / QuickTime / SimpleMovieOut / SimpleOutFileStuff.c next >
Encoding:
C/C++ Source or Header  |  1991-10-15  |  7.0 KB  |  261 lines  |  [TEXT/MPS ]

  1. /************************************
  2. *
  3. * file: SimpleOutFileStuff.c
  4. *
  5. * Program: SimpleOutMovies
  6. * This program creates a movie by spooling in PICTs from a folder
  7. * and adding adding them one at a time to the movie. When all PICTs have
  8. * been processed a sound track is added if one is present.
  9. *
  10. * This program uses the sequence compression commands
  11. * which make smaller movie files by only saving
  12. * the changes between frames
  13. *
  14. * In this file I have collected all routines having to do with
  15. * file I/O
  16. *
  17. * By Guillermo A. Ortiz 
  18. * April 1991
  19. *
  20. *    Apple Macintosh Developer Technical Support
  21. *
  22. *    Copyright © 1989-1991 Apple Computer, Inc.
  23. *    All rights reserved.
  24. *
  25. *************************************/
  26.  
  27.  
  28. #include <Types.h>
  29. #include <QuickDraw.h>
  30. #include <ToolUtils.h>
  31. #include <Events.h>
  32. #include <Controls.h>
  33. #include <Windows.h>
  34. #include <Dialogs.h>
  35. #include <Menus.h>
  36. #include <Desk.h>
  37. #include <SegLoad.h>
  38. #include <Files.h>
  39. #include <OSEvents.h>
  40. #include <Traps.h>
  41. #include <Fonts.h>
  42. #include <OSUtils.h>
  43. #include <Resources.h>
  44. #include <Memory.h>
  45. #include <Packages.h>
  46. #include <Lists.h>
  47. #include <Files.h>
  48. #include <errors.h>
  49.  
  50. /* STR# resources */
  51. #define rStrMisc        256
  52.  
  53. /* Resource ID's for Standard File Dialog */
  54. #define rGetDirectoryDLOG    1002
  55.  
  56. /* resources for the sound alert */
  57.  
  58. #define kHasSound     1
  59. #define rSoundAlert 128
  60.  
  61. #define CurDirStore        0x398        /* DirID of current directory [LONG]        */
  62. #define firstTime        -1  /* the first time our hook is called, it is passed a -1 */
  63.  
  64. OSErr OpenOneFile(PicHandle p, short index);
  65.  
  66. Handle MyGetSF2(void);
  67. OSErr MyGetSF(FSSpec *);
  68.  
  69. /* for SetDirectory sample */
  70. long            MyCurDir;
  71. Boolean            CurDirValid;
  72.  
  73. /* some globals */
  74. SFReply            reply;            /* used in all SF samples */
  75. Point            gLocation = {0x40,0x40};
  76. SFTypeList        typeList = {'FSSD'};        /* gimmie sound files */
  77.  
  78. /******************************************************************************/
  79. pascal short MyGetDirHook(item, dPtr)
  80.     short        item;
  81.     DialogPtr    dPtr;
  82. {
  83. /* Equates for the itmes that I've added */
  84. #define getDirButton 11
  85. #define getDirNowButton 12
  86. #define getDirMessage 13
  87.  
  88.     Str255    messageTitle;
  89.     short    kind;
  90.     Handle    h;
  91.     Rect    r;
  92.  
  93.     switch (item) {
  94.         case firstTime:
  95.  
  96.             /* Read in the prompt string from the resource fork, and initialize */
  97.             /* CurDirValid to FALSE. */
  98.  
  99.             GetIndString(&messageTitle,rStrMisc,3);
  100.             GetDItem(dPtr,getDirMessage,&kind,&h,&r);
  101.             SetIText(h,&messageTitle);
  102.             CurDirValid = false;
  103.             break;
  104.         case getDirButton:
  105.             if (reply.fType != 0) {
  106.                 MyCurDir = reply.fType;
  107.                 CurDirValid = true;
  108.                 return(getCancel);
  109.             };
  110.             break;
  111.         case getDirNowButton:
  112.             MyCurDir = *(long *)CurDirStore;
  113.             CurDirValid = true;
  114.             return(getCancel);
  115.     };
  116.     return(item);    /* By default, return the item passed to us. */
  117. };
  118.  
  119.  
  120. pascal Boolean FoldersOnly(p)
  121.     ParmBlkPtr p;
  122. {
  123.     /* Normally, folders are ALWAYS shown, and aren't even passed to        */
  124.     /* this file filter for judgement. Under such circumstances, it is        */
  125.     /* only necessary to blindly return TRUE (allow no files whatsoever).    */
  126.     /* However, Standard File is not documented in such a manner, and        */
  127.     /* this feature may not be TRUE in the future. Therefore, we DO check    */
  128.     /* to see if the entry passed to us describes a file or a directory.    */
  129.  
  130.     if ((p->fileParam.ioFlAttrib & 0x10) != 0)
  131.         return(false);
  132.     return(true);
  133. };
  134.  
  135.  
  136. void GetDirectory()
  137. {
  138.     
  139.     SFPGetFile(gLocation,    /* location */
  140.         "\pSpace for Rent",    /* vestigial string */
  141.         FoldersOnly,        /* fileFilter */
  142.         -1,                    /* numtypes; -1 means all */
  143.         &typeList,            /* array to types to show */
  144.         MyGetDirHook,        /* dlgHook */
  145.         &reply,                /* record for returned values */
  146.         rGetDirectoryDLOG,
  147.         nil);
  148.  
  149. };
  150.  
  151. /* here we get each picture one at the time.
  152.    This function selects the file in sequential order in the
  153.    selected folder and reads in the PICT. The PICT is stored in
  154.    a handle provided by the caller. 
  155.    
  156.    When the folder contains files that are not PICT the routine returns with
  157.    no error but sets the size of the pict handle to 10 signaling the calling
  158.    party that althought the file is there it does not contain a picture.
  159.    
  160.    Writing this was great fun.
  161.    */
  162. OSErr OpenOneFile(PicHandle p, short index)
  163. {    
  164. CInfoPBRec pb;
  165. Str255    s;
  166. OSErr     err;
  167. long    fLength;
  168. short     FRefNum;
  169.  
  170.   if (CurDirValid) { /* selection of folder went ok */
  171.       pb.hFileInfo.ioCompletion = nil;
  172.     pb.hFileInfo.ioNamePtr = &s;
  173.     pb.hFileInfo.ioVRefNum = reply.vRefNum;
  174.     pb.hFileInfo.ioFDirIndex = index;
  175.     pb.hFileInfo.ioDirID = MyCurDir;
  176.     if ( ! (err = PBGetCatInfo(&pb, false)) ) {
  177.       if (pb.hFileInfo.ioFlFndrInfo.fdType != 'PICT') 
  178.         SetHandleSize ((Handle) p, 10);/* we'll use this to signify that the file is the wrong type */
  179.       else {                                /* make sure to process only PICTs */
  180.         pb.hFileInfo.ioCompletion = nil;
  181.         pb.hFileInfo.ioNamePtr = &s;
  182.         pb.hFileInfo.ioVRefNum = reply.vRefNum;
  183.         pb.hFileInfo.filler1 = 1; /*  I only need to read */
  184.          pb.hFileInfo.ioFDirIndex = 0;                            /* These three fields */
  185.          pb.hFileInfo.ioFlAttrib =  pb.hFileInfo.filler2 = 0;    /* should make for ioMisc */
  186.         pb.hFileInfo.ioDirID = MyCurDir;
  187.         if (! (err = PBHOpen ((HParmBlkPtr)&pb, false)) ) {
  188.           FRefNum = pb.hFileInfo.ioFRefNum;
  189.           err = GetEOF(FRefNum, &fLength); /* can't fail */
  190.           fLength -= 512; /* we don't need the file header */
  191.           if ( GetHandleSize((Handle) p) < fLength)  /* resize handle */
  192.             SetHandleSize ((Handle) p, fLength);
  193.           if ( ! (err = MemError()) ) {             /* problems resizing ? */
  194.             err = SetFPos(FRefNum, fsFromStart, 512); /* jump over file header */
  195.             HLock((Handle) p);
  196.             err = FSRead(FRefNum, &fLength, (Ptr)*p); 
  197.             HUnlock((Handle) p);
  198.             err = FSClose(FRefNum);
  199.           }
  200.         }
  201.       }
  202.     }
  203.   }
  204.   return err;
  205. }
  206.  
  207. /* Here we get the sound resource (we assume only one resource in each file) 
  208.    and pass it back to add to the media.
  209.    
  210. */
  211.  
  212. Handle MyGetSF2()
  213.  
  214. {
  215. Point dlgPos = {100,100};
  216. short FRefNum, resFNum;
  217. Handle resH = nil;
  218. short itemHit;
  219. Str255 volName;
  220. short  volRef;
  221.  
  222.   if ( (itemHit = Alert(rSoundAlert, nil)) == kHasSound ) { /* make sure user has sound file */
  223.     SFGetFile(dlgPos, "\pSound file:",nil,1,&typeList,nil,&reply);
  224.     if (reply.good) {
  225.       FRefNum = CurResFile();
  226.       GetVol(volName, &volRef);
  227.       SetVol('',reply.vRefNum);
  228.       resFNum = OpenResFile(reply.fName);
  229.       if ( resH = Get1IndResource('snd ',1) ) /* assuming one resource for file */
  230.           DetachResource(resH);
  231.       CloseResFile(resFNum);
  232.       UseResFile(FRefNum);
  233.       SetVol(volName, volRef);
  234.     }
  235.   }
  236.   return resH;
  237. }
  238.  
  239. /* The code that adds a sound by reference calls this code in order to get a file where
  240.    the sound data is located; the difference is that here we need to know the offset
  241.    to the data and this makes resources kind of difficult to handle.
  242. */
  243. OSErr MyGetSF(mySpec)
  244. FSSpec   *mySpec;
  245. {
  246.  
  247. OSErr err;
  248. Point dlgPos = {100,100};
  249. short itemHit;
  250.  
  251.   if ( (itemHit = Alert(rSoundAlert, nil)) == kHasSound ) { /* make sure user has sound file */
  252.     SFGetFile(dlgPos, "\pSound file:",nil,1,&typeList,nil,&reply);
  253.     if (reply.good) {
  254.       err = FSMakeFSSpec(reply.vRefNum, 0, (unsigned char *) reply.fName, mySpec);
  255.     }
  256.   }
  257.   else 
  258.     err = fnfErr;
  259.     
  260.   return err;
  261. }